package com.iloomo.util;
/**
 * 字符串相关方法
 *
 */
public class StringUtil {

	/**
	 * 将以逗号分隔的字符串转换成字符串数组
	 * @param valStr
	 * @return String[]
	 */
	public static String[] StrList(String valStr){
	    int i = 0;
	    String TempStr = valStr;
	    String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
	    valStr = valStr + ",";
	    while (valStr.indexOf(',') > 0)
	    {
	        returnStr[i] = valStr.substring(0, valStr.indexOf(','));
	        valStr = valStr.substring(valStr.indexOf(',')+1 , valStr.length());
	        
	        i++;
	    }
	    return returnStr;
	}
	
	/**获取字符串编码
	 * @param str
	 * @return
	 */
	public static String getEncoding(String str) {      
	       String encode = "GB2312";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s = encode;      
	              return s;      
	           }      
	       } catch (Exception exception) {      
	       }      
	       encode = "ISO-8859-1";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s1 = encode;      
	              return s1;      
	           }      
	       } catch (Exception exception1) {      
	       }      
	       encode = "UTF-8";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s2 = encode;      
	              return s2;      
	           }      
	       } catch (Exception exception2) {      
	       }      
	       encode = "GBK";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s3 = encode;      
	              return s3;      
	           }      
	       } catch (Exception exception3) {      
	       }      
	      return "";      
	   }

	/**获取文件名（首字母大写）
	 * @param str
	 * @return
	 */
	public static String getfirstLetterUpperByTableName(String primaryTableName) {
		String firstLetterUpperTableName = primaryTableName.substring(0, 1).toUpperCase() + primaryTableName.substring(1);
		StringBuffer sbf2 = new StringBuffer();
		if (firstLetterUpperTableName.contains("_")) {
			String[] names = firstLetterUpperTableName.split("_");
			for (String name : names) {
				name = name.substring(0, 1).toUpperCase() + name.substring(1);
				sbf2.append(name);
			}
		} else {
			sbf2.append(firstLetterUpperTableName);
		}
		firstLetterUpperTableName = sbf2.toString();
		return firstLetterUpperTableName;
	}

	/**获取驼峰式命名
	 * @param str
	 * @return
	 */
	public static String getHumpName(String tableName){
		StringBuffer sbf = new StringBuffer();
		if(tableName.contains("_")){
			String[] names = tableName.split("_");
			for(String name : names){
				if(sbf.length() == 0){
					sbf.append(name);
				}else{
					name = name.substring(0, 1).toUpperCase() + name.substring(1);
					sbf.append(name);
				}
			}
		}else{
			sbf.append(tableName);
		}
		 return sbf.toString();
	}
	
}
